<!doctype html>
<html>
	<head>
		<meta charset="utf-8">
		<title>dvxCharts Demo</title>
		<meta name="viewport" content="width=device-width, initial-scale=1">
	</head>
	<body>
		<h2 id="wait">Loading lib....</h2>

        <link rel="stylesheet" href="https://www.dvxcharts.com/Content/dvxCharts.chart.min.css">
        <script src="https://www.dvxcharts.com/Scripts/dvxCharts.chart.min.js"></script>

		<div id="container" style="width: 1920px; height: 600px;"></div>

		<script>
			function round2(val) {
				return Math.round(val * 100) / 100;
			}

			function round3(val) {
				return Math.round(val * 1000) / 1000;
			}

			function prepData(packed) {
				console.time('prep');

				// epoch,idl,recv,send,read,writ,used,free

				const numFields = packed[0];
				packed = packed.slice(numFields + 1);

				var cpu = Array(packed.length/numFields);
				var ram = Array(packed.length/numFields);
				var tcp = Array(packed.length/numFields);

				for (let i = 0, j = 0; i < packed.length; i += numFields, j++) {
					let date = new Date(packed[i] * 60 * 1000);
					cpu[j] = [date, round3(100 - packed[i+1])];
					ram[j] = [date, round2(100 * packed[i+5] / (packed[i+5] + packed[i+6]))];
					tcp[j] = [date, packed[i+3]];
				}

				console.timeEnd('prep');

				return [cpu, ram, tcp];
			}

			function makeChart(data) {
				console.time('chart');

				var chart = new dvxCharts.Chart({
						title: {
							text: 'dvxcharts (' + data[0].length * 3 + ' points)',
						//	font: '18px sans-serif'
						},
						axes: [
							{
								type: 'linear',
								location: 'bottom',
								zoomEnabled: true
							}
						],
					//	border: { strokeStyle: '#6ba851' },
					//	tooltips: {
					//		type: 'shared'
					//	},
					/*	crosshairs: {
							enabled: true,
							hLine: false,
							vLine: { strokeStyle: '#cc0a0c' }
						},
					*/
						series: [
							{
								title: 'CPU',
								type: 'line',
								data: data[0],
								markers: null
							},
							{
								title: 'RAM',
								type: 'line',
								data: data[1],
								markers: null
							},
							{
								title: 'TCP Out',
								type: 'line',
								data: data[2],
								markers: null
							}
						]
				});

				chart.write('container');

				wait.textContent = "Done!";
				console.timeEnd('chart');
			}

			let wait = document.getElementById("wait");
			wait.textContent = "Fetching data.json (2.07MB)....";
			fetch("data.json").then(r => r.json()).then(packed => {
				wait.textContent = "Rendering...";
				let data = prepData(packed);
				setTimeout(() => makeChart(data), 0);
			});
		</script>
	</body>
</html>